package com.ubigate.CooknaviHA;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class ContentActivity extends Activity implements OnClickListener, OnCompletionListener {
	private static final String TAG = "ContentActivity";

	private RecipeDetail recipeDetail;
	
	View content_main_inform_lo;
	View content_main_ingredients_lo;
	
	View content_pick_ingredients_lo;
	
	View content_secret_method_lo;
	
	View content_preparation_lo;
	
	/** Bottom Menu Button **/
	private ImageButton btnHome = null;	//홈버튼
	private ImageButton btnTimer = null;	//타이머버튼
	private ImageButton btnMetewand = null;	//계량기준 버튼
	private ImageButton btnMyrecipe = null;	//마이레시피버튼
	private ImageButton btnBack = null;	//뒤로가기버튼

	private TextView recipe_title;	//레시피제목 텍스트뷰
	private ImageView recipe_image;	//레시피 이미지뷰
	private Button btnGuide;	//가이드 버튼
	
	//레시피 detail정보를 받아오기 위해 text선언
	private TextView recipe_basic_text;
	private TextView recipe_ingredients_text;
	private TextView pick_ingredient_text;
	private TextView secret_method_text;
	private TextView preparation_text;
	
	Boolean bGoView = false;
	private String sContentPath = "";

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
    	setContentView(R.layout.content_h);//content_h.xml화면 띄움
        
    	sContentPath = ((CooknaviApp)getApplicationContext()).LoadContentPath();	//참조될 path를 받아옴
    	Log.i(TAG, "Content Path = "+ sContentPath);

    	String sTitle = "";
    	String sImage = "";
    	
        recipeDetail = ((CooknaviApp)getApplicationContext()).getDetailedRecipe();

    	sTitle = recipeDetail.getTitle();	//레시피 제목을 받아옴
    	sImage = recipeDetail.getImage();	//레시피 이미지를 받아옴

        // Bottom menu
        btnHome = (ImageButton) findViewById(R.id.btn_home);	//xml에서 선언된 홈버튼 선언
        btnTimer = (ImageButton) findViewById(R.id.btn_timer);	//xml에서 선언된 타이머버튼 선언
        btnMetewand = (ImageButton) findViewById(R.id.btn_metewand);	//xml에서 선언된 계량기준버튼 선업
        btnMyrecipe = (ImageButton) findViewById(R.id.btn_myrecipe);	//xml에서 선언된 마이레시피버튼 선언
        btnBack = (ImageButton) findViewById(R.id.btn_back);	//xml에서 선언된 뒤로가기버튼 선언
        
        //현재 activity에서 버튼을 활성화시키기 위해 ClickListener 설정
        btnHome.setOnClickListener(this);
        btnTimer.setOnClickListener(this);
        btnMetewand.setOnClickListener(this);
        btnMyrecipe.setOnClickListener(this);
        btnBack.setOnClickListener(this);
    	
        //xml에 선언된 뷰와 버튼 선언
    	recipe_title = (TextView) findViewById(R.id.recipe_title);
    	recipe_image = (ImageView) findViewById(R.id.recipe_image);
    	btnGuide = (Button) findViewById(R.id.btn_guide);
    	btnGuide.setOnClickListener(this);//현재 activity에서 버튼을 활성화시키기 위해 ClickListener 설정
    	recipe_basic_text = (TextView) findViewById(R.id.recipe_basic_text);
    	recipe_ingredients_text = (TextView) findViewById(R.id.recipe_ingredients_text);
    	pick_ingredient_text = (TextView) findViewById(R.id.pick_ingredient_text);
    	secret_method_text = (TextView) findViewById(R.id.secret_method_text);
    	preparation_text = (TextView) findViewById(R.id.preparation_text);
    	
    	recipe_title.setText(sTitle);	//레시피별로 title설정
    	Bitmap bm = BitmapFactory.decodeFile(sContentPath+"/"+sImage);//경로에 있는 bitmap을 decode
        //recipe_image.setImageBitmap(bm);
    	Drawable dr = new BitmapDrawable(bm);
        recipe_image.setBackgroundDrawable(dr);//레시피 이미지 설정
        //레시피 세부사항 정보를 받아서 text설정
        recipe_basic_text.setText(recipeDetail.getRecipeBasicInfo());
        recipe_ingredients_text.setText(recipeDetail.getRecipeIngredient());
        pick_ingredient_text.setText(recipeDetail.getPickIngredients());
        secret_method_text.setText(recipeDetail.getSecretMethod());
        preparation_text.setText(recipeDetail.getPreparation());
    }
    
	public void onCompletion(MediaPlayer mp) {
		// TODO Auto-generated method stub
        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
        	// '맛있는 요리 부탁해요.'가 끝남
        	public void run()
        	{        		
        		//intent를 통해 현재 activity에서 ViewStepActivity로 전환
                Intent intent = new Intent(ContentActivity.this, ViewStepActivity.class);
                //intent활성화
                startActivity(intent);
	    		bGoView = false;//레시피 안내 종료
        	}
        }, 1000);//1초후에 display
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int viewId = v.getId();
		Intent intent;
		switch (viewId) {
		//가이드 버튼을 클릭했을 때
    	case R.id.btn_guide:
    		// 레시피 안내 시작 (이미 시작되었으면 무시)
    		if (bGoView == false) {
	    		bGoView = true; //레시피 안내 시작
	    		MediaPlayer mp = MediaPlayer.create(this, R.raw.recipe_start);//시작할 미디어 설정
	    		mp.setOnCompletionListener(this);//mediaplayer가 끝나면 callback하도록 설정
	    		mp.start();//mediaplayer 시작
    		}
    		break;
		// Bottom Menu
    	// 홈버튼 클릭했을 때
		case R.id.btn_home:
			if (((CooknaviApp)getApplicationContext()).getNetworkState() > 0) {//네트워크 연결상태 확인 
				// 네트워크가 연결되어 있으면 CooknaviHAActivity로
				intent = new Intent(ContentActivity.this, CooknaviHAActivity.class);
				startActivity(intent);
				finish();
			} else {
				// 네트워크가 연결되어 있지 않으면 MyrecipeActivity로
				
			}
			break;
		//타이머버튼 클릭했을 때
		case R.id.btn_timer:
			//TimerActivity로 이동
			intent = new Intent(ContentActivity.this, TimerActivity.class);
			startActivity(intent);
			break;
		//계량기준 버튼 클릭했을 때
		case R.id.btn_metewand:
			//MetewandActivity로 이동
			intent = new Intent(ContentActivity.this, MetewandActivity.class);
			startActivity(intent);
			break;
		//마이레시피 버튼 클릭했을 때
		case R.id.btn_myrecipe:
//			intent = new Intent(CooknaviHAActivity.this, WebviewActivity.class);
//			intent.putExtra("url", sSearchUrl);
//			startActivity(intent);
			break;
		//뒤로가기버튼 클릭했을 때
		case R.id.btn_back:
			onBackPressed();//전화면으로 가기
		}		
	}

}
